package ru.ifmo.itmokis.gui;

import javax.swing.JLabel;
import javax.swing.JTextArea;
import org.jfree.chart.ChartPanel;
import ru.ifmo.itmokis.sim.Clock;
import ru.ifmo.itmokis.sim.ClockListener;
import ru.ifmo.itmokis.sim.ProductionQueue;
import ru.ifmo.itmokis.domain.OrderedProduct;
import ru.ifmo.itmokis.domain.Time;
import ru.ifmo.itmokis.sim.Statistics;
import ru.ifmo.itmokis.util.ChartFactory;

/**
 *
 * @author ivan
 */
public class Controller implements ClockListener {

    private Clock clock;
    private ProductionQueue productionQueue;
    private Statistics statistics;
    // ^ Dependencies ^
    private JLabel timeOutput;
    private JTextArea productionQueueOutput;

    public Controller(Clock clock, Statistics statistics, ProductionQueue productionQueue) {
        this.clock = clock;
        this.productionQueue = productionQueue;
        this.statistics = statistics;
    }

    public ChartPanel getProdChart() {
        ChartFactory cf = new ChartFactory();
        return cf.createProductivityChart(statistics.getDataset());
    }

    public void play() {
        clock.play();
    }

    public void pause() {
        clock.pause();
    }

    public void stop() {
        clock.stop();
    }

    public void setTimeOutput(JLabel timeOutput) {
        this.timeOutput = timeOutput;
    }

    public void setProductionQueueOutput(JTextArea productionQueueOutput) {
        this.productionQueueOutput = productionQueueOutput;
    }

    @Override
    public void process(Time time) {
        timeOutput.setText(time.toString());
        StringBuilder sb = new StringBuilder();
        if (productionQueue.getCurrent() != null) {
            sb.append(productionQueue.getCurrent().toString()).append('\n');
        } else {
            sb.append("No current product\n");
        }
        sb.append("————————————————————————————————\n");
        for (OrderedProduct op : productionQueue.getPlan()) {
            sb.append(op.toString()).append('\n');
        }
        productionQueueOutput.setText(sb.toString());
    }
}
